package blog.ideablog.controller;


import blog.ideablog.dao.MessageRepository;
import blog.ideablog.model.entity.MessagePO;
import blog.ideablog.model.vo.Results;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

@RestController
public class MessageController {
    @Autowired
    private MessageRepository messageRepository;

    @ApiOperation("获取留言")
    @RequestMapping("/getMessages")
    private List<MessagePO> getMessages() {
        return messageRepository.findAll(Sort.by(Sort.Direction.DESC,"time"));
    }

    @ApiOperation("更新留言")
    @GetMapping("/postMessage")
    private String postMessage(@RequestParam String author, @RequestParam String message,@RequestParam String avatar){
        MessagePO messagePO= new MessagePO();
        messagePO.setAuthor(author);
        messagePO.setMessage(message);
        messagePO.setAvatar(avatar);
        messagePO.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        messageRepository.save(messagePO);
        return "success";

    }

    @ApiOperation("删除留言")
    @DeleteMapping("/deleteMessage/{id}")
    private Results deleteMessage(@PathVariable Long id){
        messageRepository.deleteById(id);
//        return messageRepository.findAll();
        return Results.ok("删除成功", null);
    }
}
